/*
 * Copyright (C) 2010 SuaVE Studio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.suavestudio.android.game.androtron.model;

public final class Vector2D {
	public float x;
	public float y;
	
	public Vector2D(Vector2D vector) {
		set(vector.x, vector.y);
	}
	
	public Vector2D(float x, float y) {
		set(x, y);
	}
	
	public final void set(Vector2D vector) {
		set(vector.x, vector.y);
	}

	public final void set(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public final void addBy(Vector2D vector) {
		addBy(vector.x, vector.y);
	}
	
	public final void addBy(float x, float y) {
		this.x += x;
		this.y += y;
	}
	
	public final void minusBy(Vector2D vector) {
		minusBy(vector.x, vector.y);
	}
	
	public final void minusBy(float x, float y) {
		this.x -= x;
		this.y -= y;
	}
	
	public final void multiplyBy(Vector2D vector) {
		multiplyBy(vector.x, vector.y);
	}

	public final void multiplyBy(float magnitude) {
		multiplyBy(magnitude, magnitude);
	}
	
	public final void multiplyBy(float x, float y) {
		this.x *= x;
		this.y *= y;
	}
	
	public final void divideBy(Vector2D vector) throws java.lang.ArithmeticException {
		divideBy(vector.x, vector.y);
	}

	public final void divideBy(float magnitude) throws java.lang.ArithmeticException {
		divideBy(magnitude, magnitude);
	}
	
	// TODO: Should I implement exception throwing here?  Is there any performance cost
	//		 for using exceptions?
	public final void divideBy(float x, float y) throws java.lang.ArithmeticException {
		if(x != 0.0f) this.x /= x; else throw new java.lang.ArithmeticException("Cannot divide X by zero");
		if(y != 0.0f) this.y /= y; else throw new java.lang.ArithmeticException("Cannot divide Y by zero");
	}
	
	public final void invert() {
		set(-this.x, -this.y);
	}
	
	public final void absolute() {
		set(Math.abs(this.x), Math.abs(this.y));
	}
	
	public Vector2D getAbsoluteVector() {
		return new Vector2D(Math.abs(this.x), Math.abs(this.y));
	}
}
